﻿using System.Data;
using System.Data.SQLite;
using System.Text;
using NDK.Module.Model;
using NDK.Utils;

namespace NDK.Module.DAL
{
    class IntParamServices
    {


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public IntParam GetModelByName(string Name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,参数名称,整型值,最大值,最小值 from IntParams ");
            strSql.Append(" where 参数名称=@Name ");
            SQLiteParameter[] parameters = {
					new SQLiteParameter("@Name", DbType.String,50)};
            parameters[0].Value = Name;

            IntParam model = new IntParam();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.参数名称 = ds.Tables[0].Rows[0]["参数名称"].ToString();
                if (ds.Tables[0].Rows[0]["整型值"].ToString() != "")
                {
                    model.整型值 = int.Parse(ds.Tables[0].Rows[0]["整型值"].ToString());
                }
                if (ds.Tables[0].Rows[0]["最大值"].ToString() != "")
                {
                    model.最大值 = int.Parse(ds.Tables[0].Rows[0]["最大值"].ToString());
                }
                if (ds.Tables[0].Rows[0]["最小值"].ToString() != "")
                {
                    model.最小值 = int.Parse(ds.Tables[0].Rows[0]["最小值"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }
        #region  Method

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return SQLiteUtils.GetMaxID("ID", "IntParams"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int ID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from IntParams");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = ID;

			return SQLiteUtils.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public void Add(IntParam model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into IntParams(");
			strSql.Append("ID,参数名称,整型值,最大值,最小值)");
			strSql.Append(" values (");
			strSql.Append("@ID,@参数名称,@整型值,@最大值,@最小值)");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4),
					new SQLiteParameter("@参数名称", DbType.String,50),
					new SQLiteParameter("@整型值", DbType.Int32,4),
					new SQLiteParameter("@最大值", DbType.Int32,4),
					new SQLiteParameter("@最小值", DbType.Int32,4)};
			parameters[0].Value = model.ID;
			parameters[1].Value = model.参数名称;
			parameters[2].Value = model.整型值;
			parameters[3].Value = model.最大值;
			parameters[4].Value = model.最小值;

			SQLiteUtils.ExecuteSql(strSql.ToString(),parameters);
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(IntParam model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update IntParams set ");
			strSql.Append("参数名称=@参数名称,");
			strSql.Append("整型值=@整型值,");
			strSql.Append("最大值=@最大值,");
			strSql.Append("最小值=@最小值");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@参数名称", DbType.String,50),
					new SQLiteParameter("@整型值", DbType.Int32,4),
					new SQLiteParameter("@最大值", DbType.Int32,4),
					new SQLiteParameter("@最小值", DbType.Int32,4),
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = model.参数名称;
			parameters[1].Value = model.整型值;
			parameters[2].Value = model.最大值;
			parameters[3].Value = model.最小值;
			parameters[4].Value = model.ID;

			int rows=SQLiteUtils.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from IntParams ");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = ID;

			int rows=SQLiteUtils.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool DeleteList(string IDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from IntParams ");
			strSql.Append(" where ID in ("+IDlist + ")  ");
			int rows=SQLiteUtils.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IntParam GetModel(int ID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,参数名称,整型值,最大值,最小值 from IntParams ");
			strSql.Append(" where ID=@ID ");
			SQLiteParameter[] parameters = {
					new SQLiteParameter("@ID", DbType.Int32,4)};
			parameters[0].Value = ID;

            IntParam model = new IntParam();
			DataSet ds=SQLiteUtils.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				if(ds.Tables[0].Rows[0]["ID"].ToString()!="")
				{
					model.ID=int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
				}
				model.参数名称=ds.Tables[0].Rows[0]["参数名称"].ToString();
				if(ds.Tables[0].Rows[0]["整型值"].ToString()!="")
				{
					model.整型值=int.Parse(ds.Tables[0].Rows[0]["整型值"].ToString());
				}
				if(ds.Tables[0].Rows[0]["最大值"].ToString()!="")
				{
					model.最大值=int.Parse(ds.Tables[0].Rows[0]["最大值"].ToString());
				}
				if(ds.Tables[0].Rows[0]["最小值"].ToString()!="")
				{
					model.最小值=int.Parse(ds.Tables[0].Rows[0]["最小值"].ToString());
				}
				return model;
			}
			else
			{
				return null;
			}
		}

        /// <summary>
		/// 得到一个对象实体
		/// </summary>
		public IntParam GetModel(string  name)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,参数名称,整型值,最大值,最小值 from IntParams ");
            strSql.Append(" where 参数名称=@name ");
            SQLiteParameter[] parameters = {
                    new SQLiteParameter("@name", DbType.String,20)};
            parameters[0].Value = name;

            IntParam model = new IntParam();
            DataSet ds = SQLiteUtils.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.参数名称 = ds.Tables[0].Rows[0]["参数名称"].ToString();
                if (ds.Tables[0].Rows[0]["整型值"].ToString() != "")
                {
                    model.整型值 = int.Parse(ds.Tables[0].Rows[0]["整型值"].ToString());
                }
                if (ds.Tables[0].Rows[0]["最大值"].ToString() != "")
                {
                    model.最大值 = int.Parse(ds.Tables[0].Rows[0]["最大值"].ToString());
                }
                if (ds.Tables[0].Rows[0]["最小值"].ToString() != "")
                {
                    model.最小值 = int.Parse(ds.Tables[0].Rows[0]["最小值"].ToString());
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ID,参数名称,整型值,最大值,最小值 ");
			strSql.Append(" FROM IntParams ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
            return SQLiteUtils.Query(strSql.ToString());
		}


		#endregion  Method
    }
}
